#!/bin/bash

# dst: /data/file1
usage="Usage: ./dd.sh /target/file"

if [ $# -ne 1 ]; then
	echo $usage
else
blocksize=("64K" "128K" "256K" "512K" "1M" "2M" "4M" "8M" "16M")
RECFILE="./dd.log"

touch $RECFILE
> $RECFILE  ##clear all records in log file.

for (( i = 0; i < ${#blocksize[@]}; i++ ))
do
	for cnt in 10 100 200 500 1000
	do
		echo "... ... Writing $cnt x ${blocksize[$i]}Byte to $1 ... ..."
		echo "> $cnt x ${blocksize[$i]}B" >> $RECFILE  ## '>' flag-char
		dd if=/dev/zero of=$1 count=$cnt bs=${blocksize[$i]} >> $RECFILE 2>&1
	done
done

cat $RECFILE | while read LINE  
do
	if [[ ${LINE:0:1} == ">" ]] ; then 
		echo -e "$LINE\c"
	fi
	echo $LINE | awk 'NF>4 && $2!="error" {print "\ttime consuming: "$6"s" "\tWrite speed: "$8 $9}'
done
fi